package com.example.administrator.liveapp.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;

import com.example.administrator.liveapp.ApplicationUtils;
import com.example.administrator.liveapp.R;
import com.example.administrator.liveapp.Xutils3.MyCallBack;
import com.example.administrator.liveapp.Xutils3.XUtils;
import com.example.administrator.liveapp.adapter.LiveListPageAdapter;
import com.example.administrator.liveapp.bean.LiveListBean;
import com.example.administrator.liveapp.utils.AppUrl;
import com.example.administrator.liveapp.view.FullyLinearLayoutManager;
import com.google.gson.Gson;

import org.xutils.common.util.LogUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 以前直播列表activity（已经没有用了）
 */

public class LiveListPage extends AppCompatActivity {

    private RecyclerView live_list_recycle;
    private RelativeLayout live_search_relative;
    private LiveListBean bean;
    private View loadView;//loding页面（加载页）

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ApplicationUtils applicationUtils = new ApplicationUtils();
        applicationUtils.addActivity(this);
        initView();
        data();
        Onclick();
    }

    private void initView() {
        setContentView(R.layout.activity_live_list_page);
        loadView = findViewById(R.id.loadView);
        live_list_recycle = (RecyclerView) findViewById(R.id.live_list_recycle);
        live_search_relative = (RelativeLayout) findViewById(R.id.live_search_relative);
    }

    private void data() {
        String url = AppUrl.URLS + AppUrl.LiveListURL;
        Map<String,String> map = new HashMap<>();
        XUtils.Get(url, null, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Gson gson = new Gson();
                bean = gson.fromJson(result, LiveListBean.class);
                recyclerView();
                loadView.setVisibility(View.GONE);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                LogUtil.e(ex.getMessage() + "联网请求失败");
            }
        });
    }

    private void recyclerView() {
        FullyLinearLayoutManager linearLayoutManager = new FullyLinearLayoutManager(this);
        linearLayoutManager.setScrollEnabled(true);
        live_list_recycle.setHasFixedSize(true);
        live_list_recycle.setLayoutManager(linearLayoutManager);
        LiveListPageAdapter adapter = new LiveListPageAdapter(bean.getVideo());
        live_list_recycle.setAdapter(adapter);
        adapter.setOnItemClickListener(new LiveListPageAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if (ApplicationUtils.getInstance().loginBean == null) {
                    Intent intent = new Intent();
                    intent.setClass(LiveListPage.this, WxLoginPage.class);
                    startActivity(intent);
                    LiveListPage.this.overridePendingTransition(R.anim.zoomin,R.anim.zoomout);
                    return;
                }
//                Intent intent = new Intent();
//                intent.putExtra("videoId", bean.getVideo().get(position).getVideoid());
//                intent.putExtra("consumerId",ApplicationUtils.getInstance().loginBean.getConsumerid());
//                intent.putExtra("playstatics",bean.getVideo().get(position).getPlaystatics());
//                intent.setClass(LiveListPage.this, LiveDetailsPage.class);
//                startActivity(intent);
//                overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
            }
        });
    }

    private void Onclick() {
        live_search_relative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LiveListPage.this,SearchPage.class);
                intent.putExtra("searchTid","1");
                startActivity(intent);
                overridePendingTransition(R.anim.zoomin,R.anim.zoomout);
            }
        });
    }
}
